<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item :label="$t('时间')">
              <a-date-picker
                :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择开始时间')"
                class="query-group-cust"
                v-model="queryParam.bTime"
              ></a-date-picker>
              <span class="query-group-split-cust"></span>
              <a-date-picker
                :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择结束时间')"
                class="query-group-cust"
                v-model="queryParam.eTime"
              ></a-date-picker>
            </a-form-item>
          </a-col>
          <a-col :xl="8" :lg="9" :md="10" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('查询') }}</a-button>
              <a-dropdown>
                <a-menu slot="overlay" @click="handleDateClick">
                  <a-menu-item key="1"> {{ $t('今日') }} </a-menu-item>
                  <a-menu-item key="2"> {{ $t('近三天') }} </a-menu-item>
                  <a-menu-item key="3"> {{ $t('近七天') }} </a-menu-item>
                  <a-menu-item key="4"> {{ $t('近一月') }} </a-menu-item>
                  <a-menu-item key="5"> {{ $t('近三月') }} </a-menu-item>
                  <a-menu-item key="6"> {{ $t('近半年') }} </a-menu-item>
                  <a-menu-item key="7"> {{ $t('本年') }} </a-menu-item>
                </a-menu>
                <a-button type="primary" style="margin-left: 8px">
                  {{ $t('快捷查询') }} <a-icon type="down"
                /></a-button>
              </a-dropdown>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{
                $t('重置')
              }}</a-button>
            </span>
          </a-col>
          <template v-if="toggleSearchStatus"></template>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <!-- table区域-begin -->
    <div id="printme">
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSources"
        :pagination="false"
        :loading="loading"
        :customRow="customRow"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无图片') }}</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无文件') }}</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">{{
            $t('下载')
          }}</a-button>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">{{ $t('详情') }}</a>
        </span>
      </a-table>
    </div>
    <a-row style="margin-top: 20px">
      <a-col :span="10">
        <div style="text-align:center;font-size:20px;font-weight:700;color:#000">{{$t('门店吧台销售占比')}}</div>
        <div class="down-charts" id="myChart3"></div>
      </a-col>
      <a-col :span="10">
        <div style="text-align:center;font-size:20px;font-weight:700;color:#000">{{$t('门店机台销售占比')}}</div>
        <div class="down-charts" id="myChart4"></div>
      </a-col>
    </a-row>
  </a-card>
</template>

<script>
import moment from 'moment'
import '@/assets/less/TableExpand.less'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
let myChart3, myChart4

export default {
  name: 'ChannelSalesSummary',
  mixins: [JeecgListMixin],
  components: {},
  data() {
    return {
      moment,
      description: '渠道销售汇总页面',
      // 表头
      columns: [
        {
          title: this.$t('c_门店'),
          align: 'center',
          dataIndex: 'shopName',
        },
        {
          title: this.$t('c_吧台'),
          align: 'center',
          dataIndex: 'other',
          children: [
            {
              title: this.$t('c_数量'),
              align: 'center',
              dataIndex: 'couAmount',
              customCell: (r, i) => {
                return {
                  style: {
                    textAlign: 'right',
                  },
                }
              },
            },
            {
              title: this.$t('c_占比'),
              align: 'center',
              dataIndex: 'couRate',
              customRender: (t, r) => {
                let arr = t.toString().split('.')
                if (arr.length > 1 && arr[1].length > 2) {
                  return t.toFixed(2) + '%'
                }
                return t + '%'
              },
              customCell: (r, i) => {
                return {
                  style: {
                    textAlign: 'right',
                  },
                }
              },
            },
            {
              title: this.$t('c_金额'),
              align: 'center',
              dataIndex: 'couPrice',
              customRender: (t, r) => {
                let arr = t.toString().split('.')
                if (arr.length > 1 && arr[1].length > 2) {
                  return t.toFixed(2)
                }
                return t
              },
              customCell: (r, i) => {
                return {
                  style: {
                    textAlign: 'right',
                  },
                }
              },
            },
          ],
        },
        {
          title: this.$t('c_机台'),
          align: 'center',
          dataIndex: 'machine',
          children: [
            {
              title: this.$t('c_数量'),
              align: 'center',
              dataIndex: 'macAmount',
              customCell: (r, i) => {
                return {
                  style: {
                    textAlign: 'right',
                  },
                }
              },
            },
            {
              title: this.$t('c_占比'),
              align: 'center',
              dataIndex: 'macRate',
              customRender: (t, r) => {
                let arr = t.toString().split('.')
                if (arr.length > 1 && arr[1].length > 2) {
                  return t.toFixed(2) + '%'
                }
                return t + '%'
              },
              customCell: (r, i) => {
                return {
                  style: {
                    textAlign: 'right',
                  },
                }
              },
            },
            {
              title: this.$t('c_金额'),
              align: 'center',
              dataIndex: 'macPrice',
              customRender: (t, r) => {
                let arr = t.toString().split('.')
                if (arr.length > 1 && arr[1].length > 2) {
                  return t.toFixed(2)
                }
                return t
              },
              customCell: (r, i) => {
                return {
                  style: {
                    textAlign: 'right',
                  },
                }
              },
            },
          ],
        },
        {
          title: this.$t('总计'),
          align: 'center',
          dataIndex: 'sum',
          children: [
            {
              title: this.$t('c_数量'),
              align: 'center',
              dataIndex: 'amount',
              customCell: (r, i) => {
                return {
                  style: {
                    textAlign: 'right',
                  },
                }
              },
            },
            {
              title: this.$t('c_占比'),
              align: 'center',
              dataIndex: 'rate',
              customRender: (t, r) => {
                let arr = t.toString().split('.')
                if (arr.length > 1 && arr[1].length > 2) {
                  return t.toFixed(2) + '%'
                }
                return t + '%'
              },
              customCell: (r, i) => {
                return {
                  style: {
                    textAlign: 'right',
                  },
                }
              },
            },
            {
              title: this.$t('c_金额'),
              align: 'center',
              dataIndex: 'price',
              customRender: (t, r) => {
                let arr = t.toString().split('.')
                if (arr.length > 1 && arr[1].length > 2) {
                  return t.toFixed(2)
                }
                return t
              },
              customCell: (r, i) => {
                return {
                  style: {
                    textAlign: 'right',
                  },
                }
              },
            },
          ],
        },
      ],
      url: {
        list: '/report/goods/salesSummary/list',
      },
      dataSources: [],
      isNoPageTotal: true,
      queryParam: {
        bTime: moment().format('YYYY-MM-01 00:00:00'),
        eTime: moment().format('YYYY-MM-DD 23:59:59'),
      },
      isCustom: true,
      thRows: 2,
    }
  },
  created() {},
  watch: {
    dataSource(nlist) {
      this.test1(nlist)
      this.test2(nlist)
      this.dataSources = nlist.slice()
      this.addSumRow(
        ['shopName', 'couAmount', 'couPrice', 'couRate', 'amount', 'price', 'rate', 'macAmount', 'macPrice', 'macRate'],
        this.dataSources
      )
    },
  },
  methods: {
    initDictConfig() {
      this.searchQuery()
      this.$nextTick(() => {
        myChart3 = this.$echarts.init(document.getElementById('myChart3'))
        myChart4 = this.$echarts.init(document.getElementById('myChart4'), 'light')
        myChart3.showLoading({
          text: 'loading',
          color: '#000',
          maskColor: 'rgba(255, 255, 255, 0.8)',
        })
        myChart4.showLoading({
          text: 'loading',
          color: '#000',
          maskColor: 'rgba(255, 255, 255, 0.8)',
        })
      })
    },
    customRow(r, i) {
      return {
        style: {
          fontWeight: r.shopName == this.$t('合计') ? 700 : 400,
        },
      }
    },
    addSumRow(myArr, dataSource) {
      let that = this,
        total = {}
      myArr.forEach(function (tcn, ind) {
        if (ind == 0) {
          total[tcn] = that.$t('合计')
        } else {
          total[tcn] = 0
        }
      })
      for (let key in total) {
        dataSource.forEach(function (ds) {
          if (!isNaN(total[key])) {
            total[key] += Number(ds[key])
          }
        })
      }
      dataSource.push(total)
    },
    searchReset() {
      this.queryParam = {
        bTime: moment().format('YYYY-MM-01 00:00:00'),
        eTime: moment().format('YYYY-MM-DD 23:59:59'),
        shopCode: '',
      }
      this.searchQuery()
    },
    handleDateClick(evt) {
      if (evt.key == '1') {
        this.queryParam.bTime = moment().format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '2') {
        this.queryParam.bTime = moment().subtract(2, 'day').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '3') {
        this.queryParam.bTime = moment().subtract(6, 'day').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '4') {
        this.queryParam.bTime = moment().subtract(1, 'month').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '5') {
        this.queryParam.bTime = moment().subtract(3, 'month').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '6') {
        this.queryParam.bTime = moment().subtract(6, 'month').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '7') {
        this.queryParam.bTime = moment().startOf('year').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime = moment().format('YYYY-MM-DD 23:59:59')
      }
      this.searchQuery()
    },
    test1(list) {
      let that = this
      myChart3 = that.$echarts.init(document.getElementById('myChart3'))
      myChart3.hideLoading()
      myChart3.clear()
      let data = that.dataPieAxios(list, 'couRate', 'shopName')
      myChart3.setOption({
        title: {
          show: false,
          text: that.$t('门店吧台销售占比'),
          left: 'center',
        },
        tooltip: {
          trigger: 'item',
          formatter: '{b}: {c} ({d}%)',
        },
        legend: {
          orient: 'vertical',
          show: false,
          right: 40,
          top: 'center',
          icon: 'circle',
          data: that.dataBarOrLineAxios(list, 'shopName'),
          formatter: function (name) {
            var target
            for (var i = 0, l = data.length; i < l; i++) {
              if (data[i].name == name) {
                target = data[i].value
              }
            }
            var arr = [name + '：' + target]
            return arr.join('\n')
          },
          type: 'scroll',
          pageIconColor: '#444',
          pageIconInactiveColor: '#eee',
        },
        series: [
          {
            name: that.$t('门店吧台销售占比'),
            type: 'pie',
            radius: ['0', '60%'],
            center: ['50%', '50%'],
            data: data,
            label: {
              normal: {
                show: true,
                /* position: 'inner', */
                formatter: '{b}：{d}%',
              },
            },
          },
        ],
      })
    },
    test2(list) {
      let that = this
      myChart4 = that.$echarts.init(document.getElementById('myChart4'), 'light')
      myChart4.hideLoading()
      myChart4.clear()
      let data = that.dataPieAxios(list, 'macRate', 'shopName')
      myChart4.setOption({
        title: {
          show: false,
          text: that.$t('门店机台销售占比'),
          left: 'center',
        },
        tooltip: {
          trigger: 'item',
          formatter: '{b}: {c} ({d}%)',
        },
        legend: {
          orient: 'vertical',
          show: false,
          right: 30,
          top: 'center',
          icon: 'circle',
          data: that.dataBarOrLineAxios(list, 'shopName'),
          formatter: function (name) {
            var target
            for (var i = 0, l = data.length; i < l; i++) {
              if (data[i].name == name) {
                target = data[i].value
              }
            }
            var arr = [name + '：' + target]
            return arr.join('\n')
          },
          type: 'scroll',
          pageIconColor: '#444',
          pageIconInactiveColor: '#eee',
        },
        series: [
          {
            name: that.$t('门店机台销售占比'),
            type: 'pie',
            radius: ['0', '60%'],
            center: ['50%', '50%'],
            data: data,
            label: {
              normal: {
                show: true,
                /* position: 'inner', */
                formatter: '{b}：{d}%',
              },
            },
          },
        ],
      })
    },
    dataBarOrLineAxios(list, arg, arg1) {
      let dataAxios = []
      list.forEach((lt) => {
        if (arg1 && arg1 == '1') {
          lt[arg] = Math.abs(lt[arg])
        }
        dataAxios.push(lt[arg])
      })
      return dataAxios
    },
    dataPieAxios(list, arg1, arg2) {
      let dataAxios = []
      list.forEach((lt) => {
        dataAxios.push({
          value: lt[arg1],
          name: lt[arg2],
        })
      })
      return dataAxios
    },
  },
}
</script>
<style scoped>
.down-charts {
  padding: 0;
  margin: 0;
  box-shadow: none;
  background: white;
  border-radius: 15px;
  height: 22vw;
}
</style>